<?php

class ControllerPlugin_Acl extends Zend_Controller_Plugin_Abstract {	
	const NAME = 'ControllerPlugin_Acl';
	/**
	 * validate the current user's request
	 * @param  Zend_Controller_Request_Abstract $request
     * @return void
	 */
	public function preDispatch(Zend_Controller_Request_Abstract $request) {
		//根据该用户类型，判断是否存在登陆用户
		$authMoudle = Module_Impl_Auth::getInstance();
		$loginUser = $authMoudle->isLogin();	
		
		Zend_Registry::set('loginUser',$loginUser);	
		$identity = 0;
		if(!empty($loginUser) and $loginUser instanceof Object_User ){
			$identity = $loginUser->identity;
			$tt = Module_Impl_User_Opration::getInstance ();
			$tt->setCheckBeforeUpdate(false);
			$tt->update ( $loginUser->id, array ('action_at' => Util_TimeMath::getDateTime () ) );	
		}	
		
		$acl = Module_Impl_Acl::getInstance();		
		$resouce_name = $request->getModuleName().'/'.$request->getControllerName().'/'.$request->getActionName();
		$check = $acl->isAllow($identity,$resouce_name);
		
		if(!$check){
			if($request->getModuleName() === 'default'){
				$_SESSION['message'] = 'No Allow';
				$request->setControllerName ( 'index' );
				$request->setActionName ( 'message' );
			}else{
				$request->setControllerName ( 'auth' );
				$request->setActionName ( 'login' );
			}
		}
		
		/*$module_name = $request->getModuleName();
		if(strtolower($module_name) != 'default'){
			if(empty($loginUser) or !$loginUser instanceof Object_User ){
				$request->setControllerName ( 'auth' );
				$request->setActionName ( 'login' );	
			}
		}		*/
	}
}
